<template>
  <van-button
    class="follow-btn"
    round
    size="small"
    v-if="value"
    @click="followClick"
    :loading="isLoading"
    >已关注</van-button
  >
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-else
    @click="followClick"
    :loading="isLoading"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from "@/api/user";
export default {
  name: "FollowUser",
  data() {
    return {
      isLoading: false, //是否处于关注加载中
    };
  },
  props:{
    //   是否被关注
      value:{
          type:Boolean,
          required:true
      },
    //   作者id 
      authorId:{
          type:[String,Number],
          required:true
      }
  },
  methods: {
      // 关注点击事件
    async followClick() {
      // 判断是否登录
      if (!this.$store.getters.token) return this.$toast.fail("请登录后再操作");
      // 开启loading
      this.isLoading = true;
      // 判断是关注还是取消关注
      try {
        if (this.value) {
          // 已关注 要取消
          await deleteFollow(this.authorId)
        } else {
          // 未关注 可以关注
          await addFollow({
              target:this.authorId
          })
        }
        // 更新关注或已关注
        // this.articelInfo.is_followed=!this.articelInfo.is_followed
        this.$emit('input',!this.value)
      } catch (error) {
          console.log(error);
          this.$toast.fail("操作失败")
      }
    //   关闭loading  节流问题
    this.isLoading=false
    },
  },
};
</script>

<style></style>
